package cn.org.xuanzp.accept.handler;

import cn.org.xuanzp.accept.provider.AbstractAcceptProvider;
import cn.org.xuanzp.accept.provider.AcceptProvider;
import cn.org.xuanzp.accept.provider.impl.MessageProvider;
import cn.org.xuanzp.common.command.Command;
import cn.org.xuanzp.nty.server.handler.CommandHandler;
import cn.org.xuanzp.nty.server.handler.NtyHandler;

import java.util.List;

/**
 * @Classname AcceptCommandHandler
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/9/22 16:58
 * @Created by xuanzp
 */
public class AcceptCommandHandler implements CommandHandler {

    /***
     * 发送
     */
    private NtyHandler ntyHandler;

    private AbstractAcceptProvider acceptProvider;

    public AcceptCommandHandler(CommandHandler sendCommandHandler,CommandHandler transmitCommandHandler) {
        init();
        ntyHandler = new NtyHandler(sendCommandHandler,transmitCommandHandler);
        this.acceptProvider.addProvider(new MessageProvider(this));
    }

    public AcceptCommandHandler(List<AcceptProvider> acceptProvider,NtyHandler ntyHandler) {
        this.ntyHandler = ntyHandler;
        init();
        acceptProvider.forEach(provider -> {
            this.acceptProvider.addProvider(provider);
        });
    }

    public AcceptCommandHandler(NtyHandler ntyHandler) {
        this.ntyHandler = ntyHandler;
        init();
    }

    public void addProvider(AcceptProvider acceptProvider){
        this.acceptProvider.addProvider(acceptProvider);
    }

    /***
     * 指令处理
     * @param command
     */
    @Override
    public void handler(Command command) {
        acceptProvider.accept(command);
    }

    /***
     * 初始化参数
     */
    private void init() {
        this.acceptProvider = new AbstractAcceptProvider() {};
    }

    /***
     * 消息发送
     * @param command
     */
    public void route(Command command){
        ntyHandler.route(command);
    }

    /***
     * 转发
     * @param command
     */
    public void transmit(Command command){
        ntyHandler.transmit(command);
    }

}
